const cloud = require('wx-server-sdk');

// 初始化云开发
cloud.init({
  env: 'liucheng-2gmqvcgs50e86304' // 使用指定的云环境
});

// 初始化数据库
const db = cloud.database();

exports.main = async (event, context) => {
  const {
    _id, // 要更新的订单的 _id
    user, // 用户
    manufactor, // 厂家
    orderNumber, // 订单号
    taobaoNumber, // 淘宝订单号
    advancePayment, // 预付款
    orderTime, // 下单时间
    expectDeliTime, // 预期交货时间
    realityTime, // 实际交货时间
    customerAddress, // 地址
    orderCompleted, // 是否完成
    afterSale, // 售后
    remarks, // 评论
    billingType, // 开票类型
  } = event;
  try { 
    const currentTime = new Date();

    const result = await db.collection('Order').doc(_id).update({
      data: {
        user: user,
        manufactor: manufactor,
        orderNumber: orderNumber,
        taobaoNumber: taobaoNumber,
        advancePayment: advancePayment,
        orderTime: orderTime,
        expectDeliTime: expectDeliTime,
        realityTime: realityTime,
        customerAddress: customerAddress,
        orderCompleted: orderCompleted,
        afterSale: afterSale,
        remarks: remarks,
        billingType: billingType,
        updatedAt: currentTime
      }
    });
    return {
      success: true,
      orderId: _id
    };
  } catch (err) {
    console.error('Error updating order:', err);
    return {
      success: false,
      errorMessage: err.message
    };
  }
};
